//
//  XXImageManager.swift
//  XXSwiftDemo
//
//  Created by admin on 2022/10/9.
//

import UIKit
import Kingfisher


// Kingfisher
public extension UIImageView {
   
    // 加载网络图片
    func setImg(_ url:String,_ placeholder:String? = nil){
        
        self.kf.setImage(with: kUrl(url), placeholder: kImage(placeholder ?? ""), options:nil ) { receivedSize, totalSize in
            
        } completionHandler: { image, error, cacheType, imageURL in
            
        }
    }
    
    // 重新下载图片
    func refImg(_ url:String,_ placeholder:String? = nil){
        self.kf.setImage(with: kUrl(url), placeholder: kImage(placeholder ?? ""), options:[.forceRefresh] ) { receivedSize, totalSize in
            
        } completionHandler: { image, error, cacheType, imageURL in
            
        }
    }
    
}

public extension UIButton {
   
    // 加载网络图片
    func setImg(_ url:String,_ placeholder:String? = nil){
        
        self.kf.setImage(with: kUrl(url), for: .normal, placeholder: kImage(placeholder ?? ""), options: nil) { receivedSize, totalSize in
            
        } completionHandler: { image, error, cacheType, imageURL in
            
        }
    }
    
    // 设置背景图
    func setBackgroundImage(_ url:String,_ placeholder:String? = nil){
        self.kf.setBackgroundImage(with: kUrl(url), for: .normal, placeholder: kImage(placeholder ?? ""), options: nil) { receivedSize, totalSize in
            
        } completionHandler: { image, error, cacheType, imageURL in
            
        }
    }
    
    // 重新下载图片
    func refImg(_ url:String,_ placeholder:String? = nil){
        self.kf.setImage(with: kUrl(url), for: .normal, placeholder: kImage(placeholder ?? ""), options: [.forceRefresh]) { receivedSize, totalSize in
            
        } completionHandler: { image, error, cacheType, imageURL in
            
        }
    }
    
}

class XXImageManager: NSObject {
   
    public typealias XXDownloadImageSucBlock = (UIImage,String)->Void
    public typealias XXDownloadImageFaiBlock = (Error)->Void
    
    // 下载图片并缓存
    class func downloadImage(_ imgUrl:String,suc: XXDownloadImageSucBlock? = nil,fai: XXDownloadImageFaiBlock? = nil ){
        
        ImageDownloader.default.downloadImage(with: kUrl(imgUrl)!, retrieveImageTask: nil, options: nil) { receivedSize, totalSize in
            
        } completionHandler: { image, error, url, originalData in
            if let img = image{
                // 缓存
                ImageCache.default.store(img, forKey: imgUrl)
                if let block = suc {
                    block(img,imgUrl)
                }
            }else{
                if let block = fai {
                    block(error!)
                }
            }
        }
    }
    
    // 从缓存中加载图片
    class func loadImg(_ key:String,_ res: @escaping XXDownloadImageSucBlock){
        ImageCache.default.retrieveImage(forKey: key, options: nil, completionHandler: { image, cacheType in
            if let img = image{
                if cacheType == .memory{
                    res(img,"memory")
                }else if cacheType == .disk{
                    res(img,"disk")
                }else{
                    res(img,"none")
                }
            }
        })
    }
    
    // 移除缓存图片
    class func removeImg(_ key:String){
        ImageCache.default.removeImage(forKey: key)
    }
    
    // 获取磁盘缓存大小
    class func calCacheSize (_ res:@escaping XXIntBlock){
        ImageCache.default.calculateDiskCacheSize { size in
            res(Int(size))
        }
    }
    
    // 清理缓存
    class func clearCache(){
        ImageCache.default.clearMemoryCache()
        ImageCache.default.clearDiskCache()
        ImageCache.default.cleanExpiredDiskCache()
    }
    
    // 设置缓存时间 单位 M
    class func maxCacheSize(_ size:Int){
        ImageCache.default.maxDiskCacheSize = UInt(size * 1024 * 1024)
    }
    
    // 设置缓存大小 单位 天
    class func maxCacheDay(_ day:Int){
        ImageCache.default.maxCachePeriodInSecond = TimeInterval(day * 24 * 60)
    }
}
